dojo.provide("lotex.browser");

lotex.browser.popupWindow = function (url, tag, simple) {
    d("popup window with : url="+url+" &tag="+tag+" &simple="+simple);
    try {
        var options = "";
        if (simple)
            options = 'toolbar=no,scrollbars=yes,directories=no,width=900,location=no,status=no,menubar=no,resizable=yes';

        // IE complains if the name contains spaces or periods
        tag = tag.replace(/\./g, "");
        tag = tag.replace(/\s/g, "");

        var result = window.open(url, tag, options);

        if (result != null) {
            result.focus(); //Bring the window to the front
            return false;
        }

        alert("不能弹出窗口，请检查浏览器设置！");
        //Can't popup, just reset the window location
        //lotex.browser.NavWindowToURL(url);
        return false;
    } catch (e) {
        try {

            //Mozilla will sometimes blow up opening a new window, because the URL is bad. So,
            //rebuild it here.
            var newURL = lotex.browser.folderOfURL(location.href, false) + url;

            var result2 = window.open(newURL, tag, options);
        }
        catch (e) {
            //return true, hope the link works
            return true;
        }
        return false;
    }
}

//Return the folder part of the URL - everything before the last slash, with or without the last slash.
lotex.browser.folderOfURL = function (URLString, no_slash)
{
        return URLString.slice(0,URLString.lastIndexOf('/',URLString.length-(/\/$/.test(URLString)?2:0))+(no_slash?0:1));
}

//Some browsers have issues with various ways of doing this.
lotex.browser.NavWindowToURL = function (newURL) {
    if (typeof location.assign == 'function')
        location.assign(newURL);
    else if (typeof location.replace == 'function')
        location.replace(newURL);
    else if (self.location.href)
        self.location.href = newURL;
    else
        window.location = newURL;
}
